/**
 * Created by Administrator on 2016/10/14.
 */
/**
 * Created by Administrator on 2016/10/12.
 */

//获取必要的元素
var runData = {
    go:document.getElementById('go'),//向指定方向前进
    left:document.getElementById('left'),//转向左面
    right:document.getElementById('right'),//转向右面
    top:document.getElementById('top'),//转向上面
    bottom:document.getElementById('bottom'),//转向下面
    run_div:document.getElementsByClassName('run_div')[0],
    red_div:document.getElementsByClassName('red_div')[0],
    td:document.getElementsByTagName('td'),
    car:document.getElementsByClassName('car')[0],
    do_it:document.getElementById('do_it'),
    reset:document.getElementById('reset'),
    textarea:document.getElementById('textarea'),
    row_num:document.getElementById('row_num')
};

function addEvent(ele,eventName,listener){
    if(ele.addEventListener){
        ele.addEventListener(eventName,listener,false);
    }else if(ele.attachEvent){
        ele.attachEvent('on'+eventName,listener);
    }else{
        ele['on'+eventName] = listener;
    }
}


var runDiv = {
   direction : 'north',
   turnDirection : function(direction){
        switch(direction){
            case 'left':
                runData.run_div.style.transform = 'rotate(-90deg)';
                runDiv.direction = 'west';
                break;
            case 'right':
                runData.run_div.style.transform = 'rotate(-270deg)';
                runDiv.direction = 'east';
                break;
            case 'top':
                runData.run_div.style.transform = 'rotate(0deg)';
                runDiv.direction = 'north';
                break;
            case 'bottom':
                runData.run_div.style.transform = 'rotate(-180deg)';
                runDiv.direction = 'south';
                break;
        }
//        this.go();
    },
   go : function(num){
        switch (runDiv.direction){
            case 'north':
//                alert('134')
                var n1 = (runData.run_div.offsetTop-42)/41;
                if(n1>num){
                    n1 = num;
                }
//                alert(n1);
                if(runData.run_div.offsetTop != 42){
                    runData.run_div.style.top = (runData.run_div.offsetTop-41*n1) + 'px';
                }
                break;
            case 'south':
                var n2 = (411-runData.run_div.offsetTop)/41;
                if(n2>num){
                    n2 = num;
                }
                if(runData.run_div.offsetTop != 411){
                    runData.run_div.style.top = (runData.run_div.offsetTop+41*n2) + 'px';
                }
                break;
            case 'west':
                var n3 = (runData.run_div.offsetLeft-42)/41;
                if(n3>num){
                    n3 = num;
                }
                if(runData.run_div.offsetLeft != 42){
                    runData.run_div.style.left = (runData.run_div.offsetLeft-41*n3) + 'px';
                }
                break;
            case 'east':
                var n4 = (411-runData.run_div.offsetLeft)/41;
                if(n4>num){
                    n4 = num;
                }
                if(runData.run_div.offsetLeft != 411){
                    runData.run_div.style.left = (runData.run_div.offsetLeft+41*n4) + 'px';
                }
                break;

        }

    },
    run : function(commend){
        switch(commend){
            case 'Turn Left':
                runDiv.direction = 'west';
                runDiv.turnDirection(runDiv.direction);
                break;
            case 'Turn Right':
                runDiv.direction = 'east';
                runDiv.turnDirection(runDiv.direction);
                break;
            case 'Turn Top':
                runDiv.direction = 'north';
                runDiv.turnDirection(runDiv.direction);
                break;
            case 'Turn Bottom':
                runDiv.direction = 'south';
                runDiv.turnDirection(runDiv.direction);
                break;
        }
    },
    textCommend : function(){
        var textareaValue = runData.textarea.value;
        textareaValue = textareaValue.split('\n');
        var i = 0;
        var len = textareaValue.length;
        var flag = 0;
        for(var j = 0;j<len;j++){
            var commends = textareaValue[j].slice(0,textareaValue[j].length-1);
            var number = textareaValue[j].slice(textareaValue[j].length-1);
            commends = commends.trim();
            if(commends == 'GO'||commends == 'Turn Left'||commends == 'Turn Right'||commends == 'Turn Top'||commends == 'Turn Top'||commends == 'Turn Bottom'){
            }else {
                flag++;
                runData.row_num.children[j].style.backgroundColor = 'red';
                runData.row_num.children[j].style.borderRadius = "50%";
            }
        }
            if(flag == 0){
                setInterval(function(){
                    var commends = textareaValue[i].slice(0,textareaValue[i].length-1);
                    var number = textareaValue[i].slice(textareaValue[i].length-1);
                    commends = commends.trim();
                    if(commends == 'GO'||commends == 'Turn Left'||commends == 'Turn Right'||commends == 'Turn Top'||commends == 'Turn Top'||commends == 'Turn Bottom'){
                        runDiv.move(commends, number);
                    }else {
                        runData.row_num.children[i].style.backgroundColor = 'red';
                        runData.row_num.children[i].style.borderRadius = "50%";
                    }
                    if(i<len){
                        i++;
                    }

                },500);
            }
       },
    reset:function(){
        var len = runData.row_num.children.length;
        runData.textarea.value = '';
        for(var i = 0;i<len-1;i++){
          runData.row_num.removeChild(runData.row_num.lastElementChild);
        }
        runData.row_num.children[0].style.backgroundColor = 'white';
        runData.row_num.children[0].style.borderRadius = "0%";
    },
   move : function(dire,n){
        var commends = dire;
        var number = n;
        if(commends == 'GO'){
//            alert('go')
            runDiv.go(number);
        }else if(commends == 'Turn Left'){
            runDiv.turnDirection('left');
            runDiv.go(number);
        }else if(commends == 'Turn Right'){
            runDiv.turnDirection('right');
            runDiv.go(number);
        }else if(commends == 'Turn Top'){
            runDiv.turnDirection('top');
            runDiv.go(number);
        }else if(commends == 'Turn Bottom'){
            runDiv.turnDirection('bottom');
            runDiv.go(number);
        }
    }

};

//关于textarea的行数显示
function textRowShow(){
    var row_num = document.getElementById('row_num');
    addEvent(runData.textarea,'keydown',function(e){
        var textareaValue = runData.textarea.value;
        textareaValue = textareaValue.split('\n');
        rows = textareaValue;
        if(e.keyCode == 13){
            var rowNum = textareaValue.length;
            var rowDiv = document.createElement('div');
            rowDiv.className = 'row_div';
            rowDiv.innerHTML = rowNum;
            row_num.appendChild(rowDiv);
        }
    });
}
textRowShow();
addEvent(runData.do_it,'click',function(){runDiv.textCommend();});
addEvent(runData.reset,'click',function(){
    runDiv.reset();
});
